from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

#在窗口之间切换句柄——只要没有打开新的标签页就不需要切换句柄
ChromeIns=ChromeDriverManager().install()
driver=webdriver.Chrome(service=Service(ChromeIns))

driver.get("https://www.baidu.com")
title=driver.title
url=driver.current_url

print("before:"+title)
print("before:"+url)

driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
time.sleep(3)

cur_handle=driver.current_window_handle
all_handles=driver.window_handles
time.sleep(3)

for handle in all_handles:
    if handle != cur_handle:
        driver.switch_to.window(handle)
    print(handle)

title=driver.title
url=driver.current_url

print("after:" + title)
print("after:" + url)

driver.quit()